﻿
$(function () {
    var attachFastClick = Origami.fastclick;
    attachFastClick(document.body);
    var marqueeFn;
    var $btn = $(".lotteryBtn").find("a");
    var flag = parseInt($(".counts").find("span").text());
    if (flag == 0) {
        $btn.addClass("locked");
    }
    function btnMarquee() {
        var _ul = $('.lottery_area ul')
        var _flag = _ul.attr("data-flag");
        if (_flag == '1')
        { $(".light2").show(); $(".light1").hide(); _ul.attr("data-flag", "2") }
        else
        { $(".light1").show(); $(".light2").hide(); _ul.attr("data-flag", "1") }
    }
    //抽奖代码
    var btt = 0;//起始位置
    // 转动效果
    function bt() {
        btt++;
        if (btt == 9) { btt = 1; }
        $(".lottery_area li").each(function () {
            if ($(this).attr('index') == btt) {
                $(this).find('i').hide();
                $(this).siblings().find('i').show();
                return false;
            }
        })
        return btt;
    }
    function move(end, ra, prizeType) {
        c++;
        if ((c > ra) && (btt == end)) {
            //转动结束之后的处理
            clearInterval(marqueeFn);//结束跑马灯	
            setTimeout(function () {
                $(".overlayer").show();
                $(".lottery_area li i").hide();
                if (prizeType == 'again') {
                    $(".counts").find("span").text(flag);
                }
            }, 500)
            return;
        } else {
            bt();
        }
        setTimeout(function () { move(end, ra, prizeType); }, 100)
    }
    // 抽奖按钮设置
    $(".lotteryBtn").on("click", function (e) {
        if ($btn.hasClass("disabled")) { return };
        var login = $btn.attr('data-login');
        if (login == "false") {
            $(".overlayer").show();
            get_price(-3, '', '');
            return;
        }
        c = 0;
        $btn.addClass("disabled");
        getOrderId("");
    });

    function getOrderId(msg) {
        $.ajax({
            url: ajax_url,
            timeout: 10000,
            dataType: "json",
            type: "post",
            success: function (response) {
                if (response.status == 1) {
                    marqueeFn = setInterval(btnMarquee, 500);//开始跑马灯
                    setTimeout(function () {
                        sub_ajax(response.data.RecordId, response.data.OrderDetailId);
                    }, 1000);
                } else {
                    $('.face').attr('class', 'face faild');
                    show_over(response.msg, "", 0); $(".overlayer").show();
                    $('.modal-dialog .block-help').hide();
                    var _yes = $(".confirm");
                    var _no = $(".cancel");
                    if (response.lowCredit) {
                        if (false) {
                            _yes.text('赚取更多分积').unbind().show().on("click", function (e) {
                                if (false) {
                                    window.location.href = '&uid=test123456&dbnewopen';
                                } else {
                                    window.location.href = '?uid=test123456&dbnewopen';
                                }
                            });
                        } else {
                            _yes.unbind().hide();
                        }
                    } else {
                        _yes.unbind().hide();

                    }
                    _no.text("确定").unbind().show().on("click", function (e) {
                        initBtn();
                    });
                }
            },
            error: function () {
                $('.face').attr('class', 'face faild');
                show_over("噢哦，网络解析错误！", "再试一遍吧", 0); $(".overlayer").show();
                var _yes = $(".confirm");
                var _no = $(".cancel");
                _no.unbind().hide();
                _yes.text("再试一遍").unbind().on("click", function (e) {
                    initBtn();
                });
            }
        })
    }
    function sub_ajax(recordId, orderDetailId) {
        $.ajax({
            url: ajax_query_url + '/' + recordId,
            timeout: 3000,
            data: { oDetailId: orderDetailId },
            dataType: "json",
            type: "post",
            success: function (response) {
                if (response.status <= 0) {
                    clearInterval(marqueeFn);//结束跑马灯
                    return;
                } else if (response.status == 1) {
                    var data = response.data;

                    end = parseInt(data.PrizeSequence);//结束位置
                    ra = Math.floor(Math.random() * 3 + 3) * 8;//随机量
                    move(end, ra, data.PrizeType);
                    if (data.PrizeType == 'again') {
                        get_price(data.PrizeType, data.RecordId, data.OrderDetailId);
                    } else {
                        get_price(data.PrizeType, data.PrizeTitle, data.OrderDetailId);
                    }
                    //if (typeof (data.lowcredit) != "undefined") {
                    //    $btn.data('lowcredit', data.lowcredit);
                    //}
                    if (data.IsFree == false) {
                        $(".counts").html("本次 " + $('.counts').attr('data-points') + pointUnitName + "/次");
                    }
                } else {
                    get_price('-1', response.msg, data.OrderDetailId);
                    $(".overlayer").show();
                    clearInterval(marqueeFn);//结束跑马灯	
                }
            },
            error: function (msg) {
                clearInterval(marqueeFn);//结束跑马灯
                $('.face').attr('class', 'face faild');
                show_over("噢哦，网络解析错误！", "再试一遍吧", 0); $(".overlayer").show();
                var _yes = $(".confirm"); var _no = $(".cancel");
                _no.unbind().hide();
                _yes.text("再试一遍").unbind().on("click", function (e) {
                    initBtn();
                });
            }
        });
    }
    //重置按钮
    function initBtn() {
        var $btn = $(".lotteryBtn").find("a");
        $(".overlayer").hide();
        setTimeout(function () { $btn.removeClass("disabled"); }, 100);
        var flag = parseInt($(".counts").find("span").text());
        if (flag == 0) {
            $btn.addClass("locked");
        }
    }
    //领奖
    function get_price(tg, msg, id) {
        var _yes = $(".confirm");
        var _no = $(".cancel");
        switch (tg) {
            case 'thanks'://谢谢参与
                var h = '唉呀，就差那么一点点~';
                var p = '别灰心，再玩一次！';
                _no.unbind().hide();
                _yes.text("继续抽奖").unbind().show().on("click", function (e) {
                    initBtn();
                });
                $('.face').attr('class', 'face');
                $('.modal-dialog .block-help').hide();
                show_over(h, p, 0);
                break;
            case 'again'://再抽一次
                var h = '恭喜你，中奖了！！！';
                var p = '获得免费再抽一次的机会~';
                _no.unbind().hide();
                _yes.text("再抽一次").show().unbind().on("click", function (e) {
                    initBtn();
                    c = 0;
                    setTimeout(function () { $(".lotteryBtn").find("a").addClass("disabled"); }, 150);
                    getOrderId(msg);//这里设置再抽一次的变量名
                });
                $('.face').attr('class', 'face success');
                $('.modal-dialog .block-help').hide();
                show_over(h, p, 0);
                break;
            case '-1'://错误
                var h = msg;
                var p = '下次再来哦~';
                if (msg.indexOf("今") != -1) {
                    p = '明天在来哦~'
                }
                _no.text("").unbind().hide();
                _yes.text("知道了").unbind().show().on("click", function (e) {
                    initBtn();
                });
                $('.face').attr('class', 'face error');
                $('.modal-dialog .block-help').hide();
                show_over(h, p, 0);
                break;
            case '-2'://积分不足
                var h = '分积不足'; var p = '赚够分积再来吧~';
                _yes.text("确 定").unbind().show().on("click", function (e) {
                    initBtn();
                });
                $('.face').attr('class', 'face error');
                $('.modal-dialog .block-help').hide();

                if ('') {
                    _no.text("赚取更多分积").unbind().show().on('click', function () {
                        window.location.href = '?dbnewopen';
                    });
                    show_over(h, p, 1);
                } else {
                    _no.text("").unbind().hide();
                    show_over(h, p, 0);
                }
                break;
            case '-3'://未登录
                var h = '亲，您还没有登录哦~';
                var p = '马上去登录吧！';
                _no.text("").unbind().hide();
                _yes.text("请先登录").unbind().show().on("click", function (e) {
                    initBtn();

                });
                $('.face').attr('class', 'face nologin');
                $('.modal-dialog .block-help').hide();
                show_over(h, p, 0);
                break;
            default://中奖
                var h = '恭喜你，中奖了！！！';
                var p = msg;
                _no.text("继续抽奖").unbind().show().on("click", function (e) {
                    initBtn();
                });

                _yes.text(tg == '' ? '马上查看' : "马上领奖").unbind().show().on("click", function (e) {

                    window.location.href = ajax_go_take_prize + "/" + id; //这里设置跳转页面的变量名

                    initBtn();
                });
                $('.face').attr('class', 'face success');
                $('.modal-dialog .block-help').show();
                show_over(h, p, 1);
        }
    }
    //手机端通用提示，h为标题文案，p为正文
    function show_over(h, p, a) {
        var y = parseInt((parseInt($(window).height()) / 2) - 154);
        $(".overlayer").find(".modal-dialog").css("top", y);
        $(".lottery_info").find(".lottery_tip").html(h);
        $(".lottery_info").find(".lottery_title").html(p);
        if (a == 1) {
            $(".confirm").addClass("ajax");
            $(".cancel").show();
        } else {
            $(".confirm").removeClass("ajax");
            $(".cancel").hide();
        }
    }
    $('.probability-rules header').on('click', function () {
        $(this).toggleClass('show');
        $('.probability-rules section').toggleClass('show');
    })

});